{% extends "devhub/base.html" %}

{% set title = _('Status & Versions') %}
{% block title %}{{ dev_page_title(title, addon) }}{% endblock %}

{% set current_version=addon.current_version %}
{% set latest_listed_version_including_disabled = addon.find_latest_version(channel=amo.CHANNEL_LISTED, exclude=()) %}
{% set latest_unlisted_version_including_disabled = addon.find_latest_version(channel=amo.CHANNEL_UNLISTED, exclude=()) %}
{% set latest_listed_version=addon.find_latest_version(channel=amo.CHANNEL_LISTED) %}

{% if latest_listed_version_including_disabled %}
  {% set has_listed_versions = True %}
{% endif %}
{% if latest_unlisted_version_including_disabled %}
  {% set has_unlisted_versions = True %}
{% endif %}

{% if has_listed_versions and has_unlisted_versions %}
  {% macro distro_tag(channel) %}
    {% if channel == amo.CHANNEL_LISTED %}
      <span class="distribution-tag-listed tooltip"
            title="{{ _('Listed on this site after passing code review. Automatic updates are handled by this site.') }}"
            >AMO</span>
    {% elif channel == amo.CHANNEL_UNLISTED %}
      <span class="distribution-tag-unlisted tooltip"
            title="{{ _('Signed for self-distribution. Updates are handled by you.') }}"
            >Self</span>
    {% endif %}
  {% endmacro %}
{% else %}
  {% macro distro_tag(channel) %}{% endmacro %}
{% endif %}

{% macro version_details(version, full_info=False) %}
{% set latest_version_in_channel_including_disabled = latest_listed_version_including_disabled if version.channel == amo.CHANNEL_LISTED else latest_unlisted_version_including_disabled %}
  <tr{% if version.file.status == amo.STATUS_DISABLED %} class="version-disabled"{% endif %}>
    <td>
      <strong>
        <a href="{{ url('devhub.versions.edit', addon.slug, version.id) }}"
           title="{{ _('Edit this version') }}">
          {{ _('Version {0}')|format_html(version.version) }}</a>
      </strong>
      <span title="{{ version.created|isotime }}" class="note">
        {{ version.created|date }}
      </span>
    </td>
    <td class="file-status">
      <div>
        {{ distro_tag(version.channel)}}
        <span class="file-status-text">
          {% if version.file.status == amo.STATUS_DISABLED and version.is_blocked %}
            {# Slight optimization: don't try getting blocked status for
               non-disabled versions - this avoids checking if for latest/current
               versions, which are fetched without a select_related('blockversion') #}
            {{ version.blockversion.get_user_facing_block_type_display() }}
          {% else %}
            {{ version.file.get_status_display() }}
          {% endif %}
        </span>
      </div>
      {% if version.is_user_disabled %}
        <div>
        <form method="post" action="{{ url('devhub.versions.reenable', addon.slug) }}">
            {% csrf_token %}
            <input type="hidden" name="version_id" class="version_id" value="{{ version.id }}">
            <input type="hidden" name="addon_id" class="addon_id" value="{{ addon.id }}">
            <button type="submit" class="link">{{ _('Re-enable Version') }}</button>
        </form>
        </div>
      {% endif %}
      <div>
          <a href="#" class="review-history-show" data-div="#{{ version.id }}-review-history"
            id="review-history-show-{{ version.id }}" data-version="{{ version.id }}">{{ _('Review History') }}</a>
          <a href="#" class="review-history-hide hidden">{{ _('Close Review History') }}</a>
            {% set pending_count = pending_activity_log_count_for_developer(version) %}
            {% if pending_count > 0 %}
                <b class="review-history-pending-count">{{ pending_count }}</b>
            {% endif %}
      </div>
    </td>
    <td class="file-validation">
      <ul>
          <li>
          {% if version.file.has_been_validated %}
            <ul>
              <li><a href="{{ url('devhub.file_validation', addon.slug, version.file.id) }}">
                {{ summarize_validation(version.file.validation) }}</a>
              </li>
            </ul>
          {% endif %}
          </li>
      </ul>
    </td>
    <td class="version-delete">
      <a href="#" class="remove" data-version="{{ version.id }}"
         data-can-be-disabled="{{ version.can_be_disabled_and_deleted()|int }}"
         data-is-current="{{ (version == addon.current_version)|int }}">x</a>
    </td>
  </tr>
  {% if full_info and version.channel == amo.CHANNEL_LISTED and can_request_review %}
    <tr>
      <td colspan="0" class="version-status-actions item-actions">
        {% if can_request_review %}
          <form method="post"
                action="{{ url('devhub.request-review', addon.slug) }}">
            {% csrf_token %}
            <button class="link" type="submit">{{ _('Request Review') }}</button> &middot;
          </form>
        {% endif %}
      </td>
    </tr>
  {% endif %}
  <tr>
      <td colspan="4" id="{{ version.id }}-review-history" class="review-history hidden"
            data-api-url="{{ drf_url('version-reviewnotes-list', addon.id, version.id) }}"
            data-session-id="{{ session_id }}">
          <div class="history-container">
              <div class="review-entry-loading">{{ _('Loading Review History...') }}</div>
              <div class="review-entry-failure hidden">{{ _('We had a problem retrieving review notes') }}</div>
              <div class="review-entry-loadmore hidden">
                  <p><a href="#" class="review-history-loadmore" data-div="#{{ version.id }}-review-history">{{ _('Load older...') }}</a></p>
              </div>
              <div class="review-entry-empty hidden">
                  <div>
                    <p><strong class="action">$action_label</strong> {{ _('by') }}
                    <em class="user_name">$user_name</em> <time class="timeago" datetime="$date">$date</time></p>
                    <pre>$comments</pre>
                  </div>
                  <div class="review-entry-attachment hidden">
                    <a class="attachment_url">Download Attachment</a>
                    <span class="attachment_size download-reply-attachment">($attachment_size)</span>
                  </div>
              </div>
          </div>
          <div class="dev-review-reply">
            <form class="dev-review-reply-form" action="{{ drf_url('version-reviewnotes-list', addon.id, version.id) }}"
                  data-session-id="{{ session_id }}" data-history="#{{ version.id }}-review-history" data-no-csrf>
                {# This is not a django form, an XHR goes through to the form action on submit #}
                <textarea maxlength="{{ comments_maxlength }}" name="comments" placeholder="{{ _('Leave a reply') }}"></textarea>
                <button type="submit" class="submit" >{{ _('Reply') }}</button>
            </form>
          </div>
      </td>
  </tr>
{% endmacro %}

{% block content %}
<header>
  <h2>{{ addon.name }}</h2>
</header>
<section id="edit-addon" class="primary devhub-form" role="main">
  {# Only show listing visibility if there are listed versions... or if somehow the add-on is already invisible,
     to allow developers to toggle it back so that they can upload listed versions #}
  {% if has_listed_versions or addon.disabled_by_user %}
    <h3>{{ distro_tag(amo.CHANNEL_LISTED)}} {{ _('Listing visibility') }}</h3>
    <div class="item" id="addon-current-state">
      <div class="item_wrapper">
        <label><input name="addon-state" value="listed" type="radio"
                    {% if addon.status == amo.STATUS_DISABLED %}disabled="disabled"{% endif %}
                    {% if not addon.is_disabled %}checked="checked"{% endif %}
                    data-url="{{ addon.get_dev_url('enable') }}"
                    class="enable-addon">
        {{ _("{label_open}Visible:{label_close} Visible to everyone on {site_url} and included "
             "in search results and product pages.")|format_html(
             label_open='<strong>'|safe, label_close='</strong>'|safe, site_url=settings.SITE_URL)|safe }}</label>
        <br>
        <label><input name="addon-state" value="hidden" type="radio"
                    {% if addon.status == amo.STATUS_DISABLED %}disabled="disabled"{% endif %}
                    {% if addon.is_disabled %}checked="checked"{% endif %}
                    class="disable-addon">
        {{ _("{label_open}Invisible:{label_close} Won't be included in search results, and its product page will "
              "indicate you disabled it. New version submissions for product "
              "won't be accepted in this state.")|format_html(
             label_open='<strong>'|safe, label_close='</strong>'|safe) }}</label>
      </div>
    </div>
    {% if latest_listed_version or current_version %}
        <h3>{{ distro_tag(amo.CHANNEL_LISTED)}} {{ _('Listed versions') }}</h3>
    {% endif %}
    {% if current_version %}
      <div class="item" id="current-version-status">
        <div class="item_wrapper">
          <table>
            <tr>
              <th>{{ _('Currently on AMO') }}</th>
              <th>{{ _('Status') }}</th>
              <th>{{ _('Validation') }}</th>
              <th class="version-delete">{{ _('Delete/Disable') }}</th>
            </tr>
            {{ version_details(current_version,
                               full_info=(not latest_listed_version
                                          or latest_listed_version == current_version)) }}
          </table>
        </div>
      </div>
    {% endif %}

    {% if latest_listed_version and latest_listed_version != current_version %}
      <div class="item" id="next-version-status">
        <div class="item_wrapper">
          <table>
            <tr>
              <th>{{ _('Next version of this add-on') }}</th>
              <th>{{ _('Status') }}</th>
              <th>{{ _('Validation') }}</th>
              <th class="version-delete">{{ _('Delete/Disable') }}</th>
            </tr>
            {{ version_details(latest_listed_version, full_info=True) }}
          </table>
        </div>
      </div>
    {% endif %}

    {% set other_versions_header_text =_('Other versions') %}
  {% else %}
    {% set other_versions_header_text =_('All versions') %}
  {% endif %}
  <div id="other-versions-header">
    <h3>{{ other_versions_header_text }}</h3>

    <div class="version-buttons">
      {% if can_rollback %}
      <div>
        <a href="#rollback-version" class="button version-rollback">{{ _('Rollback to a previous version') }}</a>
      </div>
      {% endif %}
      {% if can_submit %}
      <div>
        {% set version_upload_url = url('devhub.submit.version', addon.slug) %}
        <a href="{{ version_upload_url }}" class="button version-upload">{{ _('Upload a New Version') }}</a>
      </div>
      {% endif %}
    </div>
  </div>
  <div class="item" id="version-list"
       data-stats="{{ url('devhub.versions.stats', addon.slug) }}">
    <div class="item_wrapper">
      <table>
        <tr>
          <th>{{ _('Version') }}</th>
          <th>{{ _('Status') }}</th>
          <th>{{ _('Validation') }}</th>
          <th class="version-delete">{{ _('Delete/Disable') }}</th>
        </tr>

        {% for version in versions.object_list %}
          {% if version != current_version and version != latest_listed_version %}
            {{ version_details(version, full_info=version==latest_listed_version_including_disabled) }}
          {% endif %}
        {% endfor %}
      </table>
    </div>
    {% if versions.paginator.num_pages > 1 %}
      <div class="listing-footer">
        {{ versions|paginator }}
      </div>
    {% endif %}
  </div>
  {# Checks for an actual owner, not just a developer #}
  {% if check_addon_ownership(request.user, addon) and addon.can_be_deleted() %}
  <h3>{{ _('Delete Add-on') }}</h3>
  <div class="item" id="addon-delete-listing">
    <div class="item_wrapper">
      <p>{% include 'devhub/addons/listing/_delete_warning.html' %}</p>
      <a class="delete-button delete-addon" href="{{ addon.get_dev_url('versions') }}#delete-addon">{{ _('Delete Add-on') }}</a>
    </div>
  </div>
  {% endif %}
</section>

<div id="modals">

  {% if addon.can_be_deleted() %}
    <div id="modal-delete" class="modal modal-delete">
      {% include "devhub/addons/listing/delete_form.html" %}
    </div>
  {% endif %}

  <div id="modal-delete-version" class="modal modal-delete">
    <h3 data-tmpl="{{ _('Delete Version {version}') }}"></h3>
    <form method="post" action="{{ url('devhub.versions.delete', addon.slug) }}">
      <p class="current-version-warning highlight">{{ _('You are about to delete the current version of your add-on. This may cause your add-on status to change, or your listing to lose public visibility, if this is the only public version of your add-on.') }}</p>
      <p>{{ _('Deleting this version will permanently delete:') }}</p>
      <ul>
        <li id="del-files"></li>
      </ul>
      <p class="highlight warning">
      <strong>{{ _('Important:') }}</strong>
      {% trans %}
        Once a version has been deleted, you may not upload a new
        version with the same version number.
        The version will also be Restricted, which will disable and prevent any further installation in Firefox.
        Existing users can choose to re-enable the add-on.
      {% endtrans %}
      </p>
      <p>{{ _('Are you sure you wish to delete this version?') }}</p>
      {% csrf_token %}
      <div class="modal-actions">
        <input type="hidden" name="version_id" class="version_id">
        <input type="hidden" name="addon_id" class="addon_id" value="{{ addon.id }}">
        <button type="submit" class="delete-button">{{ _('Delete Version') }}</button>
        <button type="submit" class="disable-button" name="disable_version">
          {{ _('Disable Version') }}
        </button>
        {{ _('or') }} <a href="#" class="close">{{ _('Cancel') }}</a>
      </div>
    </form>
    <p class="promoted-version-warning">
      {# will be hidden by default, shown by javascript if data-can-be-disabled is falsey #}
      {% trans promoted_group_name = addon.promoted_groups().name %}
        The latest approved version of this {{ promoted_group_name }} add-on cannot be deleted or disabled
        because the previous version was not approved for {{ promoted_group_name }} promotion.
        Please contact AMO Admins if you need help with this.
      {% endtrans %}
      <a href="#" class="close">{{ _('Cancel') }}</a>
    </p>
  </div>

  {% if can_rollback %}
  {% include "devhub/includes/modal_rollback_version.html" %}}
  {% endif %}

  {% if has_listed_versions %}
  {% if not addon.disabled_by_user and not addon.is_disabled %}
  <div id="modal-disable" class="modal">
    <h3>{{ _('Hide Add-on') }}</h3>
    <form method="post" action="{{ addon.get_dev_url('disable') }}">
      <p>
        {% trans %}
          Hiding your add-on will prevent it from appearing anywhere in our
          gallery and will stop users from receiving automatic updates.
        {% endtrans %}
      </p>
      {% if latest_listed_version and latest_listed_version.is_unreviewed %}
        <p>
            {% trans %}
            The files awaiting review will be disabled and you will need to
            upload new versions.
            {% endtrans %}
        </p>
      {% endif %}
      <p>
        {% trans %}
          Are you sure you wish to hide your add-on?
        {% endtrans %}
      </p>
      {% csrf_token %}
      <input type="hidden" name="version_id" class="version_id">
      <input type="hidden" name="addon_id" class="addon_id" value="{{ addon.id }}">
      <p>
        <button type="submit">
          {{ _('Hide Add-on') }}
        </button>
        {{ _('or') }} <a href="#" class="close">{{ _('Cancel') }}</a>
      </p>
    </form>
  </div>
  {% endif %}
  {% endif %}
</div>

{% include "devhub/includes/addons_edit_nav.html" %}
{% endblock %}
